package com.yonyou.cyxdms.sysAuth.service.controller.notification;

import com.yonyou.cyxdms.sysAuth.service.dto.notification.NotificationFeedbackDTO;
import com.yonyou.cyxdms.sysAuth.service.service.notification.NotificationFeedbackService;
import com.yonyou.cyxdms.sysAuth.service.vo.notification.NotificationSearchVO;
import com.yonyou.cyxdms.sysAuth.service.vo.notification.NotificationUserVO;
import org.springframework.web.bind.annotation.*;
import com.yonyou.cyx.framework.controller.base.BaseController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

import org.springframework.http.MediaType;

import com.yonyou.cyx.framework.util.bean.ApplicationContextHelper;
import com.yonyou.cyxdms.framework.bean.dto.user_info.LoginInfoDto;

import io.swagger.annotations.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * @author adu
 * @since 2019-10-14
 */
@Api(value = "通知通告反馈", tags = {"通知通告反馈"})
@RestController
@RequestMapping("/notification/notificationFeedback")
public class NotificationFeedbackController extends BaseController {

	private final Logger logger = LoggerFactory.getLogger(NotificationFeedbackController.class);

	@Autowired
	public NotificationFeedbackService notificationFeedbackService;

	/**
	 * 分页查询数据
	 *
	 * @param queryCondition 查询条件
	 * @return com.baomidou.mybatisplus.core.metadata.IPage 分页对象
	 * @author chengliang
	 * @since 2019-09-25
	 */
	@ResponseBody
	@GetMapping
	@SuppressWarnings("unchecked")
	@ApiOperation(value = "通知通告反馈分页查询", notes = "通知通告反馈分页查询", httpMethod = "GET",
			produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@ApiImplicitParams({@ApiImplicitParam(value = "用户id", name = "userId", required = true, dataType = "string", paramType = "header")})
	public IPage<Map<String,Object>> getList (Map<String,Object> queryCondition, @RequestParam
		("currentPage") int currentPage, @RequestParam("pageSize") int pageSize) {
		LoginInfoDto loginInfo = ApplicationContextHelper.getBeanByType(LoginInfoDto.class);
		if(queryCondition!=null){
			queryCondition.put("appId",loginInfo.getAppId());
		}
		Page<Map<String,Object>> page = new Page(currentPage, pageSize);
		return notificationFeedbackService.selectPageBysql(page, queryCondition);
	}

	/**
	 * 按ID查询数据
	 *
	 * @param id 数据主键ID
	 * @return  Map<String,Object> 数据对应的map 结构
	 * @author chengliang
	 * @since 2019-09-25
	 */
	@ResponseBody
	@GetMapping(value="/{id}")
	@ApiOperation(value = "通知通告反馈明细查询", notes = "通知通告反馈明细查询", httpMethod = "GET",
			produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@ApiImplicitParams({@ApiImplicitParam(value = "用户id", name = "userId", required = true, dataType = "string", paramType = "header")})
	public Map<String,Object> getById(@PathVariable("id") Long id) {
		return notificationFeedbackService.getById(id);
	}

	/**
	 * 进行数据新增
	 *
	 * @param notificationFeedbackDTO 需要保存的DTO
	 * @return Map<String,Object> 返回的数据map
	 * @author chengliang
	 * @since 2019-09-25
	 */
	@ResponseBody
	@PostMapping("/{notificationId}")
	@ResponseStatus(HttpStatus.OK)
	@ApiOperation(value = "通知通告反馈新增", notes = "通知通告反馈新增", httpMethod = "POST",
			produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@ApiImplicitParams({@ApiImplicitParam(value = "用户id", name = "userId", required = true, dataType = "string", paramType = "header")})
    public Integer insert(@PathVariable("notificationId") Long notificationId, @RequestBody NotificationFeedbackDTO notificationFeedbackDTO) {
        LoginInfoDto loginInfo = ApplicationContextHelper.getBeanByType(LoginInfoDto.class);
        Integer userId = loginInfo.getUserId().intValue();
        Integer countId = notificationFeedbackService.selectbyuid(notificationId.intValue(), userId);
        //有则修改，无则添加
        notificationFeedbackDTO.setStatus(1);
        notificationFeedbackDTO.setNotificationId(notificationId);
        notificationFeedbackDTO.setFeedbackUserName(loginInfo.getUserName());
        notificationFeedbackDTO.setFeedbackUser(userId.longValue());
        if (countId <= 0) {
            notificationFeedbackService.insert(notificationFeedbackDTO);
        }
        logger.debug("notificationFeedbackDTO:{}", notificationFeedbackDTO);
        return notificationId.intValue();
	}

	/**
	 * 进行数据修改
	 *
	 * @param id 需要修改数据的ID
	 * @param notificationFeedbackDTO 需要保存的DTO
	 * @return Map<String,Object> 返回的数据map
	 * @author chengliang
	 * @since 2019-09-25
	 */
	@ResponseBody
	@PutMapping(value="/{id}")
	@ResponseStatus(HttpStatus.CREATED)
	@ApiOperation(value = "通知通告反馈修改", notes = "通知通告反馈修改", httpMethod = "PUT",
			produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@ApiImplicitParams({@ApiImplicitParam(value = "用户id", name = "userId", required = true, dataType = "string", paramType = "header")})
	public Map<String,Object> update(@PathVariable("id") Long id, @RequestBody NotificationFeedbackDTO notificationFeedbackDTO) {
		logger.debug("notificationFeedbackDTO:{}",notificationFeedbackDTO);
		return notificationFeedbackService.update(id,notificationFeedbackDTO);
	}

	/**
	 * 根据id删除对象
	 *
	 * @param id 需要修改数据的ID
	 * @author chengliang
	 * @since 2019-09-25
	 */
	@DeleteMapping(value="/{id}")
	@ResponseStatus(HttpStatus.NO_CONTENT)
	@ApiOperation(value = "通知通告反馈删除", notes = "通知通告反馈删除", httpMethod = "DELETE",
			produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@ApiImplicitParams({@ApiImplicitParam(value = "用户id", name = "userId", required = true, dataType = "string", paramType = "header")})
	public boolean deleteById(@PathVariable("id") Long id){
		logger.debug("删除ID:{}",id);
		notificationFeedbackService.deleteById(id);
		return true;
	}

	/**
	 * 根据IDs批量删除对象
	 *
	 * @param ids 需要修改数据的ID集合
	 * @author chengliang
	 * @since 2019-09-25
	 */
	@DeleteMapping(value="/batch/{ids}")
	@ResponseStatus(HttpStatus.NO_CONTENT)
	@ApiOperation(value = "通知通告反馈批量删除", notes = "通知通告反馈批量删除", httpMethod = "DELETE",
			produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@ApiImplicitParams({@ApiImplicitParam(value = "用户id", name = "userId", required = true, dataType = "string", paramType = "header")})
	public boolean deleteByIds(@PathVariable("ids") String ids){
		logger.debug("删除ID:{}",ids);
		notificationFeedbackService.deleteBatchIds(ids);
		return true;
	}

	/**
	 * 导出excel表格
	 * @param request
	 * @param response
	 */
	@ApiOperation(value="导出",notes="导出Excel文件")
	@ApiImplicitParams({
			@ApiImplicitParam(value="用户ID",name="userId",required=true,dataType="string",paramType="header"),
	})
	@GetMapping(value="/export/excel")
    public void exportExcel(@RequestParam String type, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> queryParam = new HashMap<>();
        queryParam.put("type", type);
        notificationFeedbackService.exportExcel(queryParam, request, response);
	}

	/**
	 * 用户登录通知通告查看
	 * @param queryCondition
	 */
	@ApiOperation(value="用户登录通知通告查看",notes="用户登录通知通告查看")
	@ApiImplicitParams({
			@ApiImplicitParam(value="用户ID",name="userId",required=true,dataType="string",paramType="header"),
	})
	@GetMapping(value="/publis")
	public IPage<Map<String,Object>> getNotificationList(Map<String,Object> queryCondition, @RequestParam
			("currentPage") int currentPage, @RequestParam("pageSize") int pageSize) {

		LoginInfoDto loginInfo = ApplicationContextHelper.getBeanByType(LoginInfoDto.class);
		if(queryCondition!=null){
			queryCondition.put("appId",loginInfo.getAppId());
		}
		Page<Map<String,Object>> page = new Page(currentPage, pageSize);

		return notificationFeedbackService.getNotificationList(page, queryCondition);
	}


	/**
	 * 厂端员工公告查看
	 * 李贺
	 * 2019/12/4
	 */
	@ApiOperation("厂端公告查看")
	@ApiImplicitParams({
			@ApiImplicitParam(value = "用户ID", name = "userId", required = true, dataType = "string", paramType = "header"),
			@ApiImplicitParam(value = "用户appId", name = "appId", required = true, dataType = "string", paramType = "header")
	})
	@GetMapping(value = "/factoryUser")
	public Page<NotificationUserVO> factoryUserNotification(@RequestParam(value = "currentPage", defaultValue = "1") Integer currentPage,
															@RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
															NotificationSearchVO notificationSearchVO) {
		Page<NotificationUserVO> page = new Page<>(currentPage, pageSize);
		return notificationFeedbackService.factoryUserNotification(page, notificationSearchVO);
	}

	/**
	 * 厂端员工公告查看后新增一条状态
	 * 李贺
	 * 2019/12/4
	 */
	@ApiOperation("厂端员工公告查看后新增一条状态")
	@ApiImplicitParams({
			@ApiImplicitParam(value = "用户ID", name = "userId", required = true, dataType = "string", paramType = "header"),
			@ApiImplicitParam(value = "用户appId", name = "appId", required = true, dataType = "string", paramType = "header")
	})
	@GetMapping(value = "/addFactoryBack")
	public NotificationUserVO addFactoryBack(@RequestParam(value = "notificationId") Long notificationId) {
		return notificationFeedbackService.addFactoryBack(notificationId);
	}
}